<?php
namespace App\Libraries\writelog;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

class WriteLog
{
    /**
     * @param $channel 频道
     * @param $output  输出格式"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
     * @param $message 消息
     * @param array $context 参数
     * @return bool
     */
    public static function write($channel,$output=null,$message,array $context = array()){
        if(empty($output)){
            $output = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
        }
        $Logger = new Logger($channel);
        $formatter = new LineFormatter($output);
        $Logger->pushHandler((new StreamHandler(storage_path().'/logs/'.$channel.'/'.date('Y-m-d').'.log', Logger::INFO))->setFormatter($formatter));
        $Logger->info($message,$context);
        return true;
    }

    //API接口输出日志
    public static function outLog($message,array $context = array()){

        if(is_array($message)){
            $message = json_encode($message,JSON_UNESCAPED_UNICODE);
        }
        $output = "%datetime% > %message%\n %context% \n";
        self::write('out',$output,$message,$context);
        return true;
    }
    //订单日志传入传出
    public static function recordLog($name,$message,array $context = array()){

        if(is_array($message)){
            $message = json_encode($message,JSON_UNESCAPED_UNICODE);
        }
        $output = "%datetime% > %message%\n %context% \n";
        self::write($name,$output,$message,$context);
        return true;
    }
    
}